/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import Datatypes.DataRegistro;
import Datatypes.DataVersion;
import Fabricas.Factory;
import Interfaces.IPerfil;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

/**
 *
 * @author dalep0
 */
public class JIFVerRegistros extends javax.swing.JInternalFrame {

    private Factory f = null;
    private IPerfil ip = null;
    
    //private Thread ThreadCargar = null;
    
    final javax.swing.SwingWorker worker = new javax.swing.SwingWorker(){
        
        private List<DataRegistro> lista = new ArrayList();
        
        @Override
        protected Object doInBackground() throws Exception {
            
            while(true){
                                  
                  cargarRegistros();
                  Thread.sleep(10000);
                
            }
        
        }
        private void cargarRegistros() throws Exception{
        
            lista.clear();
            jTable1.repaint();
            this.lista = ip.listarRegistros();
            DefaultTableModel tm = (DefaultTableModel) jTable1.getModel();
            if(lista.size() > 0){
            int rows = 0;

                for (DataRegistro dr : lista) {

                    String ip = dr.getIp();
                    String navegador = dr.getNavegador();
                    String url = dr.getUrl();
                    String fecha = dr.getFecha().getString();
                    tm.addRow(new Object[rows]);
                    tm.setValueAt(dr.getIp(), rows, 0);
                    tm.setValueAt(dr.getNavegador(), rows, 1);
                    tm.setValueAt(dr.getUrl(), rows, 2);
                    tm.setValueAt(dr.getFecha().getString(), rows, 3);


                    rows++;
                }
            }
            else{

                jLabelEstado.setText("No hay registros.");

            }

        }
 
        
        
    };
    
    public JIFVerRegistros(Factory f) throws Exception{
       
        this.f = f;
        ip = f.getIPerfil();
        //this.lista = new ArrayList();
        initComponents();
        
        jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        TableColumn col = jTable1.getColumnModel().getColumn(0);
        col.setPreferredWidth(130);
        col = jTable1.getColumnModel().getColumn(1);
        col.setPreferredWidth(500);
        col = jTable1.getColumnModel().getColumn(2);
        col.setPreferredWidth(400);
        
        worker.execute(); 
        //TableColumnAdjuster tca = new TableColumnAdjuster(jTable1);
        //tca.adjustColumns();
        /*this.ThreadCargar = new Thread(){
        public void run(){
            
            while(true){
                try{
                    
                    Thread.sleep(10000);
                    cargarRegistros();
                    
                }
                
                catch(Exception e){


                }
            }
        }
                
        
        };
        
        ThreadCargar.start();*/
        
        
    }
   
    
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButtonSalir = new javax.swing.JButton();
        jLabelEstado = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();

        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        jScrollPane1.setToolTipText("");
        jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "IP Usuario", "Navegador", "URL", "Fecha"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jButtonSalir.setText("Salir");
        jButtonSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSalirActionPerformed(evt);
            }
        });

        jLabelEstado.setText(" ");

        jLabel1.setFont(new java.awt.Font("Ubuntu", 1, 15)); // NOI18N
        jLabel1.setText("Registros");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 625, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabelEstado)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButtonSalir, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 15, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 315, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelEstado)
                    .addComponent(jButtonSalir))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSalirActionPerformed
       
        this.dispose();
        worker.cancel(true);
        JIFInicial.BloquearJFInicial();
        
    }//GEN-LAST:event_jButtonSalirActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonSalir;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabelEstado;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
}
